/** 路由配置、守卫 */
import router from './router'
import { Route } from 'vue-router'
import { userModule } from '@/store/modules/user'
import { menuModule } from '@/store/modules/menu'
import { getToken } from '@/utils/cookie'

/**
 * 拦截未登录的访问
 * 获取用户信息
 */
router.beforeEach(async (to: Route, _: Route, next: any) => {
    const token = getToken()
    if (token) {
        if (userModule.userInfo.length === 0) {
            await userModule.GetUserInfo()
        }
        if (menuModule.homeMenuIndex !== to.name) {
            await menuModule.setHomeMenuIndex(to.name as string)
        }
        next()
    } else {
        if (to.path === '/login') {
            next()
        } else {
            next({ name: 'login' })
        }
    }
})
